//
// 作者：AKA_GGbond
// 时间：2025/7/18
// 邮箱：evain_wl@163.com
//

#include "W_RadiusForm.h"
#include <QVBoxLayout>
#include <QGraphicsDropShadowEffect>
#include <QEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QPainterPath>

W_RadiusForm::W_RadiusForm(QWidget *parent)
        : QWidget(parent)
{
    padding = 15;

    widget = new QWidget;
    widget->setObjectName("centerWidget");
    widget->setStyleSheet("QWidget#centerWidget{border-radius:15px;}");

    QVBoxLayout *lay_bg = new QVBoxLayout;
    lay_bg->addWidget(widget);

    setAttribute(Qt::WA_TranslucentBackground, true);
    setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);

    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    shadow->setOffset(0, 0);
    shadow->setColor(QColor("#50e3f8"));
    shadow->setBlurRadius(padding);
    widget->setGraphicsEffect(shadow);
    lay_bg->setContentsMargins(padding,padding,padding,padding);

    setLayout(lay_bg);
    setMouseTracking(true);
    widget->setMouseTracking(true);
}

W_RadiusForm::~W_RadiusForm()
{
}

void W_RadiusForm::mouseMoveEvent(QMouseEvent *event)
{
    QPoint point = event->pos();
    if (resizeEnable)
    {
        updatePaddingRect();
        if (rectRight.contains(point))
        {
            setCursor(Qt::SizeHorCursor);
        }
        else if (rectBottom.contains(point))
        {
            setCursor(Qt::SizeVerCursor);
        }
        else if (rectRightBottom.contains(point))
        {
            setCursor(Qt::SizeFDiagCursor);
        }
        else
        {
            setCursor(Qt::ArrowCursor);
        }
    }

    //根据当前鼠标位置,计算XY轴移动了多少
    int offsetX = point.x() - lastPos.x();
    int offsetY = point.y() - lastPos.y();

    //根据按下处的位置判断是否是移动控件还是拉伸控件
    if (moveEnable)
    {
        if (mousePressType == pressType::PressedButNotInPadding)
        {
            move(x() + offsetX, y() + offsetY);
        }
    }

    if (resizeEnable)
    {
        if (mousePressType == pressType::PressedRight)
        {
            setGeometry(rectX, rectY, rectW + offsetX, rectH);
        }
        else if (mousePressType == pressType::PressedBottom)
        {
            setGeometry(rectX, rectY, rectW, rectH + offsetY);
        }
        else if (mousePressType == pressType::PressedRightBottom)
        {
            int resizeW = rectW + offsetX;
            int resizeH = rectH + offsetY;
            setGeometry(x(), y(), resizeW, resizeH);
        }
        else
        {
        }
        updatePaddingRect();
    }

    QWidget::mouseMoveEvent(event);
}

void W_RadiusForm::mousePressEvent(QMouseEvent *event)
{
    rectX = x();
    rectY = y();
    rectW = width();
    rectH = height();
    lastPos = event->pos();

    if (rectRight.contains(lastPos))
    {
        mousePressType = pressType::PressedRight;
    }
    else if (rectBottom.contains(lastPos))
    {
        mousePressType = pressType::PressedBottom;
    }
    else if (rectRightBottom.contains(lastPos))
    {
        mousePressType = pressType::PressedRightBottom;
    }
    else
    {
        mousePressType = pressType::PressedButNotInPadding;
    }
    QWidget::mousePressEvent(event);
}

void W_RadiusForm::mouseReleaseEvent(QMouseEvent *event)
{
    mousePressType = pressType::NoPressed;
    setCursor(Qt::ArrowCursor);
    updatePaddingRect();
    QWidget::mouseReleaseEvent(event);
}

void W_RadiusForm::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 抗锯齿

    // 1. 绘制阴影（使用 QGraphicsDropShadowEffect 会影响性能，改用 QPainter 绘制）
    QPainterPath shadowPath;
    shadowPath.addRoundedRect(rect().adjusted(padding, padding, -padding, -padding), 15, 15);

    QColor shadowColor("#50e3f8");
    for (int i = 0; i < 5; i++)  // 模拟模糊阴影
    {
        shadowColor.setAlpha(50 - i * 10);
        painter.setPen(QPen(shadowColor, 1));
        painter.drawPath(shadowPath);
    }

    // 2. 绘制圆角背景（可以是纯色或渐变）
    QPainterPath bgPath;
    bgPath.addRoundedRect(rect().adjusted(padding, padding, -padding, -padding), 15, 15);

    // 使用纯色背景（示例：半透明白色）
    painter.fillPath(bgPath, QColor(255, 255, 255, 200));

    // 或者使用渐变背景（示例：蓝紫渐变）
    /*
    QLinearGradient gradient(0, 0, width(), 0);
    gradient.setColorAt(0, QColor(15, 12, 41));
    gradient.setColorAt(0.5, QColor(48, 43, 99));
    gradient.setColorAt(1, QColor(36, 36, 62));
    painter.fillPath(bgPath, gradient);
    */
}

void W_RadiusForm::setLayout(QLayout *layout)
{
    QWidget::setLayout(layout);
}

void W_RadiusForm::SetCentralWidgetLayout(QLayout *layout)
{
    if(widget->layout())
        return;

    int left{0},top{0},right{0},bottom{0};
    layout->getContentsMargins(&left, &top, &right, &bottom);
    layout->setContentsMargins(std::max(left,padding),std::max(top,padding),
                               std::max(right,padding),std::max(bottom,padding));
    widget->setLayout(layout);
}

void W_RadiusForm::updatePaddingRect()
{
    int width = this->width();
    int height = this->height();
    int padding = 20;

    rectRight = QRect(width - padding, padding, padding, height - padding * 2);
    rectBottom = QRect(padding, height - padding, width - padding * 2, padding);
    rectRightBottom = QRect(width - padding, height - padding, padding, padding);
}
